//
//  UITableView+ZJEmptyView.m
//  ZJEmptyViewDemo
//
//  Created by Mr.Zhang on 2019/2/21.
//  Copyright © 2019 Mr.Zhang. All rights reserved.
//

#import "UITableView+ZJEmptyView.h"

#import "Reachability.h"

#import <objc/runtime.h>

#import "ZJEmptyView.h"

static char *const kReloadBtnAction = "KReloadBtnAction";

@implementation UITableView (ZJEmptyView)

+ (void)load {
    
    Method old = class_getInstanceMethod(self, @selector(reloadData));
    Method new = class_getInstanceMethod(self, @selector(zj_reloadData));
    
    method_exchangeImplementations(old, new);
}

- (void)setShowNoDataEmptyView:(BOOL)zj_showNoDataEmptyView {
    objc_setAssociatedObject(self, @selector(showNoDataEmptyView), @(zj_showNoDataEmptyView), OBJC_ASSOCIATION_ASSIGN);
}

- (BOOL)showNoDataEmptyView{
    return [objc_getAssociatedObject(self, _cmd) boolValue];
}

- (void)setReloadBtnAction:(void (^)(void))reloadBtnAction
{
    objc_setAssociatedObject(self, kReloadBtnAction, reloadBtnAction, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (void (^)(void))reloadBtnAction
{
    return objc_getAssociatedObject(self, kReloadBtnAction);
}


- (void)zj_reloadData {
    
    [self zj_reloadData];
    
    if (self.mj_header.isRefreshing || ![self showNoDataEmptyView]) {
        return;
    }
    
    NSInteger totalRows = self.visibleCells.count;
    
    if (totalRows == 0) {
        
        for (NSInteger section = 0; section < self.numberOfSections; section++) {
            NSInteger rows = [self numberOfRowsInSection:section];
            totalRows += rows;
        }
    }
    
    if (totalRows > 0) {
        
        self.mj_header.hidden = NO;
        
        [self removeEmptyView];
        
    }else{
        
        [self removeEmptyView];
        
        @weakify(self);
        
        ZJEmptyView *view = [[ZJEmptyView alloc]initWithFrame:CGRectMake(0, [self isNetWork] ? (self.tableHeaderView != nil ? self.tableHeaderView.bottom : 0) : 0, SCREEN_WIDTH, self.height - ([self isNetWork] ? (self.tableHeaderView != nil ? self.tableHeaderView.bottom : 0) : 0) + self.frame.origin.y) type:[self isNetWork] ? EmptyType_NoData :  EmptyType_NoNetWork clickBlock:^{
            if (self_weak_.reloadBtnAction) {
                self_weak_.reloadBtnAction();
            }
        }];
        view.backgroundColor = self.backgroundColor;
        view.imageName = [self isNetWork] ? kLocalizedString(@"zanwushuju_c", nil) : kLocalizedString(@"duanwang_c", nil);
        [self addSubview:view];
        
        self_weak_.mj_header.hidden = ![self isNetWork];
        
        self.tableFooterView = [UIView new];
    }
}

- (void) reloadAction{}

- (BOOL)isNetWork {
    
    Reachability *reach = [Reachability reachabilityForInternetConnection];
    NetworkStatus status = [reach currentReachabilityStatus];
    return status != NotReachable ? YES : NO;
}

- (void) removeEmptyView {
    
    [self.subviews enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(__kindof UIView * _Nonnull subview, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([subview isKindOfClass:[ZJEmptyView class]]) {
            [subview removeFromSuperview];
            *stop = YES;
        }
    }];
}

@end
